package com.arykow.engine.onwebapp.client;

import java.util.List;

import com.arykow.engine.onwebapp.client.page.FeedPage;
import com.arykow.engine.onwebapp.client.page.HomePage;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.regexp.shared.MatchResult;
import com.google.gwt.regexp.shared.RegExp;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;

public class ApplicationEntryPoint implements EntryPoint {
	private final ApplicationServiceAsync greetingService = GWT.create(ApplicationService.class);

	private final FeedsContainer feedsContainer = new FeedsContainer(this);
	private final ToolbarContainer toolbarContainer = new ToolbarContainer(this);
	private final SimplePanel pageContainer = new SimplePanel();
	private final FeedPage feedPage = new FeedPage(this);
	private final HomePage homePage = new HomePage(this);

	public void onModuleLoad() {
		History.addValueChangeHandler(new ValueChangeHandler<String>() {
			public void onValueChange(ValueChangeEvent<String> event) {
				updatePage(event.getValue());
			}

		});

		/*
		 * / DockPanel panel = new DockPanel(); panel.add(toolbarContainer,
		 * DockPanel.NORTH); panel.add(feedsContainer, DockPanel.WEST);
		 * panel.add(pageContainer, DockPanel.CENTER);
		 * RootPanel.get().add(panel); /
		 */
		RootPanel.get("toolbarContainer").add(toolbarContainer);
		RootPanel.get("feedsContainer").add(feedsContainer);
		RootPanel.get("pageContainer").add(pageContainer);
		updateFeedsContainer();
		History.fireCurrentHistoryState();
	}

	public void updatePage(String token) {
		RegExp pattern = RegExp.compile("feed/([0-9]+)$");
		if (pattern.test(token)) {
			MatchResult matcher = pattern.exec(token);
			Long feedId = Long.valueOf(matcher.getGroup(1));
			updatePageWithFeedId(feedId);
			return;
		}
		updatePageWithHomePage();
	}

	private void updatePageWithHomePage() {
		pageContainer.setWidget(homePage);
	}

	public void updatePageWithFeedId(Long feedId) {
		greetingService.getFeed(feedId, new AsyncCallback<Feed>() {
			public void onSuccess(Feed result) {
				feedPage.update(result);
				pageContainer.setWidget(feedPage);
			}

			public void onFailure(Throwable caught) {
			}
		});
	}

	public void updateFeedsContainer() {
		greetingService.getFeeds(new AsyncCallback<List<Feed>>() {
			public void onSuccess(List<Feed> result) {
				feedsContainer.updateFeeds(result);
			}

			public void onFailure(Throwable caught) {
			}
		});
	}

	public void addFeed(String text) {
		greetingService.addFeed(text, new AsyncCallback<Long>() {
			public void onSuccess(Long result) {
				if (result != null) {
					History.newItem("feed/" + result);
				}
				updateFeedsContainer();
			}

			public void onFailure(Throwable caught) {
			}
		});

	}
}
